package 剑指offer;

import leetcode.day_2020_12_14_12_21.TreeNode;

public class Offer37_JZ61 {
    int index = -1;
    StringBuilder stringBuilder = new StringBuilder();
    // 未通过
    String Serialize(TreeNode root) {
        if (root == null) {
            stringBuilder.append("#，");
            return stringBuilder.toString();
        }
        stringBuilder.append(root.val + ",");
        Serialize(root.left);
        Serialize(root.right);
        return stringBuilder.toString();
    }

    String Serialize2(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        if(root == null){
            sb.append("#,");
            return sb.toString();
        }
        sb.append(root.val + ",");
        sb.append(Serialize(root.left));
        sb.append(Serialize(root.right));
        return sb.toString();
    }

    TreeNode Deserialize(String str) {
        index++;
        //int len = str.length();
        //if(index >= len){
        //    return null;
        // }
        String[] strr = str.split(",");
        TreeNode node = null;
        if(!strr[index].equals("#")){
            node = new TreeNode(Integer.valueOf(strr[index]));
            node.left = Deserialize(str);
            node.right = Deserialize(str);
        }
        return node;
    }
}
